package com.lanou.beautifulwardrobe.Home.Adapter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.DetailMobileDesc;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class ParameterGridViewAdapter extends BaseAdapter {
    private List<DetailMobileDesc> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();

    public void addData(List<DetailMobileDesc> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        DetailViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_product_detail_item, null);
            holder = new DetailViewHolder();
            holder.productDetailIv = (ImageView) convertView.findViewById(R.id.productDetailIv);

            convertView.setTag(holder);
        } else {
            holder = (DetailViewHolder) convertView.getTag();
        }
        if (datas != null && datas.size() > 0) {
            DetailMobileDesc desc = datas.get(position);

            getBitmap(desc.getContent(),holder.productDetailIv);
            Log.i("lanou",desc.getContent()+"/////");
        }

//        holder.productDetailIv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(parent.getContext(), SpaceImageDetailActivity.class);
//                intent.putExtra("images", (ArrayList<String>) imgurl);
//                intent.putExtra("position", position);
//                int[] location = new int[2];
//                Holder.productDetailIv.getLocationOnScreen(location);
//                intent.putExtra("locationX", location[0]);
//                intent.putExtra("locationY", location[1]);
//                intent.putExtra("width", imageView.getWidth());
//                intent.putExtra("height", imageView.getHeight());
//                parent.getContext().startActivity(intent);
//            }
//        });
        return convertView;
    }

    class DetailViewHolder {
        private ImageView productDetailIv;

    }

    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}